#关键词话题	关键词类别1	关键词类别2	关键词类别3	具体关键词
#关键词
#词	情绪分类

import  pandas as pd
keyword_file = r"【Social Listening】关键词节点情绪词_240531UPD.xlsx"

df = pd.read_excel(keyword_file, sheet_name="关键词")
df = df.fillna('')
Cs = [c for c in df.columns if c in "关键词话题	关键词类别1	关键词类别2	关键词类别3	具体关键词"]
keyword_df = df[Cs]
print(keyword_df)
import time

def time_decorator(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()  # 记录开始时间
        result = func(*args, **kwargs)  # 执行函数
        end_time = time.time()  # 记录结束时间
        d  =round(end_time - start_time,2)
        print(f"函数 {func.__name__} 耗时: {d} 秒")
        return result
    return wrapper


keyword_df_tag_cls3 = keyword_df.drop_duplicates(subset=['关键词类别3'])
del keyword_df_tag_cls3['具体关键词']
print(keyword_df.columns)
keyword_df['关键词信息']  = keyword_df['关键词话题'] + '-' + keyword_df['关键词类别1'] + '-' + keyword_df['关键词类别2'] + '-' + keyword_df['关键词类别3']

print(len(keyword_df), len(keyword_df['具体关键词'].unique()))

kw_list = list(keyword_df['具体关键词'].unique())
import jieba
for w in kw_list:
    jieba.add_word(w,500)
print(len(kw_list))
@time_decorator
def get_contain_kw_df(df_plat,col,kw_list):
    sub = df_plat[df_plat[col].str.contains('|'.join(kw_list), na=False)]
    return sub
@time_decorator
def process_data_under_plat(df_plat,kw_list):
    col = 'text_merge'
    set_ = set(kw_list)
    sub = get_contain_kw_df(df_plat,col,kw_list)
    if len(sub)<1000:
        sub['words'] = sub['text_merge'].apply(lambda x:set(jieba.lcut(x)))
        sub['关键词'] =  sub['words'].apply(lambda x:list(x.intersection(set_)))
        final_df =  sub[sub['关键词'].apply(len) >0]
        if  len(final_df)>0:

            final_df = final_df.explode('关键词')
        else:
            final_df = None
        return  final_df
    else:
        dfs = []
        for w in kw_list:
            sub_for_w = sub[sub[col].str.contains(w)]
            if len(sub_for_w)>0:
                #print(len(sub_for_w),'sub for w ')
                sub_for_w['关键词'] =  w
                dfs.append(sub_for_w)
        if dfs:
            final_df = pd.concat(dfs)
        else:
            final_df = None
        return final_df
from en2zh import platform2zh
for plat in platform2zh.values():
#plat = '微医'
    print(plat)
    file_path_tmp = f'df2/搜索结果_{plat}.csv'
    #df_plat = pd.read_excel(f'df2/搜索结果_{plat}.xlsx')
    df_plat = pd.read_csv(file_path_tmp)#.head(10000)
    df_plat = df_plat.fillna('')
    df_plat['text_merge'] = df_plat['标题']+df_plat['内容']
    final_df =  process_data_under_plat(df_plat,kw_list)
    if final_df is not None:
        merged_on_diff_key = pd.merge(final_df, keyword_df, left_on='关键词', right_on='具体关键词')
        merged_on_diff_key.sample(1000).to_excel(f'keywordDetail/{plat}关键词抽样1000条.xlsx')
        #merged_on_diff_key = merged_on_diff_key[merged_on_diff_key['标黄']=='是']

        class3 = pd.value_counts(merged_on_diff_key['关键词类别3'].values).to_dict()
        data  = [(k,v) for k,v in class3.items()]
        df= pd.DataFrame(data,columns=['类别3','数据量'])

        df = pd.merge(df, keyword_df_tag_cls3, left_on='类别3', right_on='关键词类别3')
        df.to_excel(f'count/类别3级统计{plat}.xlsx')
    else:
        print(plat,'没有')